<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/10 0010
 * Time: 下午 3:39
 */
namespace app\admin\controller;
use \think\Db;
class Banner extends Admin{
    //当前模块基础信息
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '网站Banner管理',
                'description' => '设置网站内Banner',
            ),
            'menu' => array(
                array(
                    'name' => 'Banner列表',
                    'url' => url('bannerList')
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加Banner',
                    'url' => url('bannerAdd'),
                ),
            ),
        );
    }

    //Bnanner列表
    public function bannerList(){
        $list = Db::name('banner')->order('id desc')->paginate(10);
        $this->view->list = $list->all();
        $this->view->page = $list->render();
        return $this->fetch();
    }

    //Banner添加
    public function bannerAdd(){
        if($this->request->isPost()){
            $data = input('post.');
            if($data['id']){
                $content = 'Banner图修改';$status = 2;
                Db::name('banner')->where('id',$data['id'])->update($data);
            }else{
                $content = 'Banner图添加';$status = 1;
                $data['create_time'] = time();
                Db::name('banner')->insert($data);
            }
            $this->log($content,$status);
            return ajaxReturn(200,'操作成功',url('bannerList'));
        }
        $this->view->info = Db::name('banner')->where('id',input('id'))->find();
        return $this->fetch();
    }

    //Banner删除
    public function del(){
        $content = 'Banner图删除';$status = 3;
        if($image = Db::name('banner')->where('id',input('id'))->value('image')){
            if(!unlink(ROOT_PATH.'/public'.$image)) {
                return ajaxReturn('500','内置错误，请尽快联系管理员！');
            }
        }
        $this->log($content,$status);
        Db::name('banner')->delete(input('id'));
        return ajaxReturn('200');
    }
}